package com.jetgege.smartbox.Me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.jetgege.smartbox.Base.BaseActivity;
import com.jetgege.smartbox.R;
import com.jetgege.smartbox.Tools.Common;
import com.kaopiz.kprogresshud.KProgressHUD;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class ModifyPWDActivity extends BaseActivity {


    @BindView(R.id.modifypwd_toolbar)
    Toolbar modifypwdToolbar;
    @BindView(R.id.et_ModifyPWDOldPWD)
    EditText etModifyPWDOldPWD;
    @BindView(R.id.ib_ModifyPWDOldPWDClear)
    ImageButton ibModifyPWDOldPWDClear;
    @BindView(R.id.tv_OldPWDErrorTips)
    TextView tvOldPWDErrorTips;
    @BindView(R.id.et_ModifyPWDNewPWD)
    EditText etModifyPWDNewPWD;
    @BindView(R.id.ib_ModifyPWDNewPWDClear)
    ImageButton ibModifyPWDNewPWDClear;
    @BindView(R.id.tv_NewPWDErrorTips)
    TextView tvNewPWDErrorTips;
    @BindView(R.id.et_ModifyPWDNewPWDConfirm)
    EditText etModifyPWDNewPWDConfirm;
    @BindView(R.id.ib_ModifyPWDNewPWDConfirmClear)
    ImageButton ibModifyPWDNewPWDConfirmClear;
    @BindView(R.id.tv_ConfirmPWDErrorTips)
    TextView tvConfirmPWDErrorTips;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_modify_pwd;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initValue();
    }

    private void initValue() {

        modifypwdToolbar.setTitle("修改密码");
        setSupportActionBar(modifypwdToolbar);
        modifypwdToolbar.setNavigationIcon(R.drawable.bar_back_btn);
        modifypwdToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
            }
        });

        //调出键盘
        Timer timer = new Timer(true);
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) etModifyPWDOldPWD.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(etModifyPWDOldPWD, 0);
            }
        }, 400);
        imputView();
    }

    private void imputView()
    {
        etModifyPWDOldPWD.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    ibModifyPWDOldPWDClear.setVisibility(View.VISIBLE);
                } else ibModifyPWDOldPWDClear.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etModifyPWDNewPWD.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    ibModifyPWDNewPWDClear.setVisibility(View.VISIBLE);
                } else ibModifyPWDNewPWDClear.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etModifyPWDNewPWDConfirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    ibModifyPWDNewPWDConfirmClear.setVisibility(View.VISIBLE);
                } else ibModifyPWDNewPWDConfirmClear.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.ib_ModifyPWDOldPWDClear, R.id.ib_ModifyPWDNewPWDClear, R.id.ib_ModifyPWDNewPWDConfirmClear, R.id.bt_ModifyPWDForgetPWD})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_ModifyPWDOldPWDClear:
                etModifyPWDOldPWD.setText("");
                tvOldPWDErrorTips.setText("");
                break;
            case R.id.ib_ModifyPWDNewPWDClear:
                etModifyPWDNewPWD.setText("");
                tvNewPWDErrorTips.setText("");
                break;
            case R.id.ib_ModifyPWDNewPWDConfirmClear:
                etModifyPWDNewPWDConfirm.setText("");
                tvConfirmPWDErrorTips.setText("");
                break;
            case R.id.bt_ModifyPWDForgetPWD:
                finish();
                startActivity(new Intent(ModifyPWDActivity.this,ForgetPWDActivity.class));
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.modify_pwd, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        modifyTips();
        return true;
    }
    private void modifyTips() {
        final KProgressHUD  modifyHUD = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在修改密码...")
                .setCancellable(true)
                .show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                modifyHUD.setLabel("密码修改成功...");
                Common.dismissHUD(modifyHUD);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                        overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
                    }
                },1200);
            }
        },1000);
    }
}
